<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>fprintf</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_000_008_804">&nbsp;</a>NAME</h4><blockquote>
fprintf, printf, snprintf, sprintf - print formatted output
</blockquote><h4><a name = "tag_000_008_805">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="stdio.h.html">stdio.h</a>&gt;

int fprintf(FILE *<i>stream</i>, const char *<i>format</i>, ...);
int printf(const char *<i>format</i>, ...);
int snprintf(char *s, size_t <i>n</i>, const char *<i>format</i>, ...);
int sprintf(char *s, const char *<i>format</i>, ...);
</code>
</pre>
</blockquote><h4><a name = "tag_000_008_806">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>fprintf()</i>
function places output on the named output
<i>stream</i>.
The
<i>printf()</i>
function places output on the standard output stream
<i>stdout</i>.
The
<i>sprintf()</i>
function places output followed by the null byte, '\0',
in consecutive bytes starting at
<i>*s</i>;
it is the user's responsibility to ensure that
enough space is available.
<p>
<i>snprintf()</i>
is identical to
<i>sprintf()</i>
with the addition of the
<i>n</i>
argument, which states the size of the buffer referred to by
<i>s</i>.
<p>
Each of these functions
converts, formats and prints its arguments
under control of the
<i>format</i>.
The
<i>format</i>
is a character string, beginning and ending in its initial shift state, if
any.  The
<i>format</i>
is composed of zero or more directives:
<i>ordinary characters</i>,
which are simply copied to the output stream and
<i>conversion specifications</i>,
each of which results in the fetching of zero or more arguments.  The results
are undefined if there are insufficient arguments for the
<i>format</i>.
If the
<i>format</i>
is exhausted while
arguments remain, the excess
arguments are evaluated but are otherwise ignored.
<p>
Conversions can be applied to the
<i>n</i>th
argument after the
<i>format</i>
in the argument list, rather than to the next unused argument.
In this case, the conversion character % (see below) is replaced
by the sequence
<i>%n$</i>
where n is a decimal integer in the range [1,&nbsp;{NL_ARGMAX}],
giving the position of the argument in the argument list.
This feature provides for the definition of format strings that
select arguments in an order appropriate to specific languages
(see the EXAMPLES section).
<p>
In format strings
containing the
<i>%n$</i>
form of conversion specifications, numbered
arguments in the argument list can be referenced from the format string
as many times as required.
<p>
In format strings containing the % form of conversion specifications,
each argument in the argument list is used exactly once.
<p>
All forms of the
<i>fprintf()</i>
functions allow for the insertion of
a language-dependent radix character in the output string.
The radix character is defined
in the program's locale (category LC_NUMERIC).
In the POSIX locale, or in a locale where
the radix character is not defined,
the radix character defaults to a period (.).
<p>
Each conversion specification is introduced by the % character
or by the character sequence
<i>%n$</i>,
after which the following appear in sequence:
<ul>
<li>
Zero or more
<i>flags</i>
(in any order), which modify the meaning of the conversion specification.
<p>
<li>
An optional minimum
<i>field width</i>.
If the converted value has fewer bytes than the field
width, it will be padded with spaces by default
on the left; it will be padded on the right, if the
left-adjustment flag (-),
described below, is given to the field width.
The field width takes the form of an asterisk (*), described
below, or a decimal integer.
<p>
<li>
An optional
<i>precision</i>
that gives the minimum number of digits to appear for the d, i, o, u, x
and X conversions; the number of digits to appear after
the radix character for the e, E and f
conversions; the maximum number of significant digits for the g and G
conversions; or the maximum number of bytes to be printed from a string in s
&nbsp;and S
&nbsp;conversions.  The precision takes the form of a period (.) followed either
by an asterisk (*), described below, or an optional decimal digit string,
where a null digit string is treated as 0.  If a precision appears with any
other conversion character, the behaviour is undefined.
<p>
<li>
An optional h specifying that a following d, i, o, u, x or X
conversion character applies to a type
<b>short int</b>
or type
<b>unsigned short int</b>
argument (the argument will have been promoted according to the
integral promotions, and its value will be converted to type
<b>short int</b>
or
<b>unsigned short int</b>
before printing); an optional h
specifying that a following n
conversion character applies to a pointer to a type
<b>short int</b>
argument; an optional l (ell) specifying that a following d, i, o, u, x or X
conversion character applies to a type
<b>long int</b>
or
<b>unsigned long int</b>
argument; an optional l (ell) specifying that a following n
conversion character applies to a pointer to a type
<b>long int</b>
argument; or an optional L specifying that a following e, E, f, g or G
conversion character applies to a type
<b>long double</b>
argument.
If an h, l or L appears with any other conversion character, the behaviour is
undefined.
<br>
<p>
<li>
An optional l specifying that a following c conversion character
applies to a
<b>wint_t</b>
argument; an optional l specifying that a following s conversion
character applies to a pointer to a 
<b>wchar_t</b>
argument.
<p>
<li>
A
<i>conversion character</i>
that indicates the type of conversion to be applied.
<p>
</ul>
<p>
A field width, or precision, or both, may be indicated by an
asterisk (*).
In this case an argument of type
<b>int</b>
supplies the field width or precision.
Arguments specifying field
width, or precision, or both must appear in that order
before the argument, if any, to be converted.
A negative field width is taken as a - flag followed by a
positive field width.
A negative precision is taken as if the precision were omitted.
&nbsp;In format strings containing the
<i>%n$</i>
form of a conversion specification, a field width or precision may be
indicated by the sequence
<i>*m$</i>,
where
<i>m</i>
is a decimal integer in the range [1,&nbsp;{NL_ARGMAX}] giving
the position in the argument list (after the format argument) of an integer
argument containing the field width or precision, for example:
<pre>
<code>
printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);
</code>
</pre>
<p>
The
<i>format</i>
can contain either numbered argument specifications
(that is,
<i>%n$</i>
and
<i>*m$</i>),
or unnumbered argument specifications (that is, % and *),
but normally not both.
The only exception to this is that %%
can be mixed with the
<i>%n$</i>
form.
The results of mixing numbered and unnumbered argument
specifications in a
<i>format</i>
string are undefined.
When numbered argument specifications are used, specifying the
<i>N</i>th
argument requires that all the leading arguments,
from the first to the (<i>N-1</i>)th, are
specified in the format string.
<p>
The flag characters and their meanings are:
<dl compact>

<dt>'<dd>The integer portion of the result of a decimal conversion
(%i, %d, %u, %f, %g or %G)
will be formatted with thousands' grouping characters.
For other conversions the behaviour is undefined.
The non-monetary grouping character is used.

<dt>-<dd>The result of the conversion will be left-justified within the field.
The conversion will be right-justified if this flag is not
specified.

<dt>+<dd>The result of a signed
conversion will always begin with a sign (+ or -).
The conversion will begin with a sign only when a negative value
is converted if this flag is not specified.


<dt>space<dd>If the first character of a signed conversion is not a sign or if
a signed conversion results in no characters, a space
will be prefixed to the result.
This means that if the space and +
flags both appear, the space flag will be ignored.

<dt>#<dd>This flag specifies that the value is to be converted
to an alternative form.
For o conversion, it increases the precision (if necessary) to force
the first digit of the result to be 0.
For x or X conversions, a non-zero result will have 0x (or 0X)
prefixed to it.
For e, E, f, g or G
conversions, the result will always contain a radix character,
even if no digits follow the radix character.
Without this flag, a radix character appears
in the result of these conversions only if a digit
follows it.
For g and G conversions, trailing zeros will
<i>not</i>
be removed from the result
as they normally are.
For other conversions, the behaviour is undefined.

<dt>0<dd>For d, i, o, u, x, X, e, E, f, g and G
conversions, leading zeros (following any indication of sign or
base) are used to pad to the field width; no space padding is
performed.
If the 0 and - flags both appear, the 0 flag will be ignored.
For d, i, o, u, x and X
conversions, if a precision is specified, the 0
flag will be ignored.
If the 0 and ' flags both appear, the grouping
characters are inserted before zero padding.
For other conversions, the behaviour is undefined.

</dl>
<p>
The conversion characters and their meanings are:
<dl compact>

<dt>d,&nbsp;i<dd>The <b>int</b> argument is converted to a signed decimal
in the style <b>[</b>-<b>]</b><i>dddd</i>.
The precision specifies the minimum number of digits to appear;
if the value being converted can be represented in fewer digits,
it will be expanded with leading zeros. The default precision is
1. The result of converting 0 with an explicit precision
of 0 is no characters.

<dt>o<dd>The <b>unsigned int</b> argument is converted to unsigned octal format
in the style <i>dddd</i>.
The precision specifies the minimum number of digits to appear; if
the value being converted can be represented in fewer digits, it will be
expanded with leading zeros. The default precision is 1. The result of
converting 0 with an explicit precision of 0 is no characters.

<dt>u<dd>The <b>unsigned int</b> argument is converted to unsigned decimal format
in the style
<i>dddd</i>.
The precision specifies the minimum number of digits to appear; if
the value being converted can be represented in fewer digits, it will be
expanded with leading zeros. The default precision is 1. The result of
converting 0 with an explicit precision of 0 is no characters.


<dt>x<dd>The <b>unsigned int</b> argument is converted to unsigned hexadecimal format
in the style
<i>dddd</i>;
the letters abcdef are used.
The precision specifies the minimum number of digits to appear; if
the value being converted can be represented in fewer digits, it will be
expanded with leading zeros. The default precision is 1. The result of
converting 0 with an explicit precision of 0 is no characters.


<dt>X<dd>Behaves the same as the x conversion character except that
letters ABCDEF are used instead of abcdef.

<dt>f<dd>The
<b>double</b>
argument is converted to decimal notation in the style
<b>[</b>-<b>]</b><i>ddd.ddd</i>, where the number of digits after the
radix character is equal to the precision specification.
If the precision is missing, it is taken as 6; if the precision is
explicitly 0 and no # flag is present, no radix character appears.
If a radix character appears, at least one digit appears
before it.
The value is rounded to the appropriate number of digits.

The
<i>fprintf()</i>
family of functions may make available character string representations
for infinity and NaN.

<dt>e, E<dd>The
<b>double</b>
argument is converted in the style
<b>[</b>-<b>]</b><i>d.ddd</i>e<i>&plusmn;dd</i>,
where there is one digit before the radix character
(which is non-zero if the argument is non-zero) and the number of
digits after it is equal to the precision; if the precision is
missing, it is taken as 6; if the precision is 0 and no # flag
is present, no radix character appears.
The value is rounded to the appropriate number of digits.
The E conversion character will produce a number with E instead of e
introducing the exponent.
The exponent always contains at least two digits.
If the value is 0, the exponent is 0.

The
<i>fprintf()</i>
family of functions may make available character string representations
for infinity and NaN.


<dt>g, G<dd>The
<b>double</b>
argument is converted in the style f or e (or in the style E
in the case of a G conversion character),
with the precision specifying the number of significant digits.
If an explicit precision is 0, it is taken as 1.
The style used depends on the value converted; style e (or E)
will be used only if the exponent resulting from such a
conversion is less than -4 or greater than or equal to the
precision.
Trailing zeros are removed from the fractional portion of the
result; a radix character appears only if it is followed
by a digit.

The
<i>fprintf()</i>
family of functions may make available character string representations
for infinity and NaN.


<dt>c<dd>The
<b>int</b>
argument is converted to an
<b>unsigned char</b>,
and the resulting byte is written.

If an l (ell) qualifier is present, the
<b>wint_t</b>
argument is converted as if by an ls conversion specification with no
precision and an argument that points to a two-element array of type
<b>wchar_t</b>,
the first element of which contains the 
<b>wint_t</b>
argument to the ls conversion specification and the second
element contains a null wide-character.


<dt>s<dd>The argument must be a pointer to an array of
<b>char</b>,
Bytes from the array are written up to (but not including) any
terminating null byte.
If the precision is specified, no
more than that many bytes are written.
If the precision is not specified or is greater than the size of
the array, the array must contain a null byte.

If an l (ell) qualifier is present, the argument must be a pointer to 
an array of type
<b>wchar_t</b>.
Wide-characters from the array are converted to characters
(each as if by a call to the
<i><a href="wcrtomb.html">wcrtomb()</a></i>
function, with the conversion state described by an
<b>mbstate_t</b>
object initialised to zero before the first wide-character is
converted) up to and including a terminating null wide-character.  The
resulting characters are written up to (but not including)
the terminating null character (byte).  If no precision is specified,
the array must contain a null wide-character.   If a precision is
specified, no more than that many characters (bytes) are written
(including shift sequences, if any), and the array must contain a null
wide-character if, to equal the character sequence length
given by the precision, the function would need to access a 
wide-character one past the end of the array.  In no case is a partial
character written.

<dt>p<dd>The argument must be a pointer to
<b>void</b>.
The value of the pointer is converted to a sequence of printable
characters, in an implementation-dependent manner.

<dt>n<dd>The argument must be a pointer to an integer into which is written
the number of bytes written to the output so far by this call to one of the
<i>fprintf()</i>
functions.
No argument is converted.


<dt>C<dd>Same as lc.

<dt>S<dd>Same as ls.

<dt>%<dd>Print a %; no argument is converted.
The entire conversion specification must be %%.

</dl>
<p>
If a conversion specification does not match one of the
above forms, the behaviour is undefined.
<p>
In no case does a non-existent or small field width
cause truncation of a field;
if the result of a conversion is wider than the field width,
the field is simply expanded to contain the conversion result.
Characters generated by
<i>fprintf()</i>
and
<i><a href="printf.html">printf()</a></i>
are printed as if
<i><a href="fputc.html">fputc()</a></i>
had been called.
<p>
The
<i>st_ctime</i>
and
<i>st_mtime</i>
fields of the file will be marked for update between the call to
a successful execution of
<i>fprintf()</i>
or
<i><a href="printf.html">printf()</a></i>
and the next successful completion of a call to
<i><a href="fflush.html">fflush()</a></i>
or
<i><a href="fclose.html">fclose()</a></i>
on the same stream or a call to
<i><a href="exit.html">exit()</a></i>
or
<i><a href="abort.html">abort()</a></i>.
</blockquote><h4><a name = "tag_000_008_807">&nbsp;</a>RETURN VALUE</h4><blockquote>
Upon successful completion, these functions
return the number of bytes transmitted
excluding the terminating null in the case of
<i>sprintf()</i>
or
<i>snprintf()</i>
or a negative value if an output error was encountered.
<p>
If the value of
<i>n</i>
is zero on a call to
<i>snprintf()</i>,
an unspecified value less than 1 is returned.
</blockquote><h4><a name = "tag_000_008_808">&nbsp;</a>ERRORS</h4><blockquote>
For the conditions under which
<i>fprintf()</i>
and
<i><a href="printf.html">printf()</a></i>
will fail and may fail, refer to
<i><a href="fputc.html">fputc()</a></i>
or
<i><a href="fputwc.html">fputwc()</a></i>.
<p>
In addition, all forms of
<i>fprintf()</i>
may fail if:
<dl compact>

<dt>[EILSEQ]<dd>
A wide-character code that does not correspond to a valid character has been
detected.

<dt>[EINVAL]<dd>
There are insufficient arguments.

</dl>
<p>
In addition,
<i><a href="printf.html">printf()</a></i>
and
<i>fprintf()</i>
may fail if:
<dl compact>

<dt>[ENOMEM]<dd>
Insufficient storage space is available.

</dl>
</blockquote><h4><a name = "tag_000_008_809">&nbsp;</a>EXAMPLES</h4><blockquote>
To print the language-independent date and time format, the
following statement could be used:
<pre>
<code>
printf (format, weekday, month, day, hour, min);
</code>
</pre>
For American usage,
<i>format</i>
could be a pointer to the string:
<pre>
<code>
"%s, %s %d, %d:%.2d\n"
</code>
</pre>
producing the message:
<pre>
<code>
Sunday, July 3, 10:02
</code>
</pre>
whereas for German usage,
<i>format</i>
could be a pointer to the string:
<pre>
<code>
"%1$s, %3$d. %2$s, %4$d:%5$.2d\n"
</code>
</pre>
producing the message:
<pre>
<code>
Sonntag, 3. Juli, 10:02
</code>
</pre>
</blockquote><h4><a name = "tag_000_008_810">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
If the application calling
<i>fprintf()</i>
has any objects of type
<b>wint_t</b>
or
<b>wchar_t,</b>
it must also include the header
<i><a href="wchar.h.html">&lt;wchar.h&gt;</a></i>
to have these objects defined.
</blockquote><h4><a name = "tag_000_008_811">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_008_812">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="fputc.html">fputc()</a></i>,
<i><a href="fscanf.html">fscanf()</a></i>,
<i><a href="setlocale.html">setlocale()</a></i>,
<i><a href="wcrtomb.html">wcrtomb()</a></i>,
<i><a href="stdio.h.html">&lt;stdio.h&gt;</a></i>,
<i><a href="wchar.h.html">&lt;wchar.h&gt;</a></i>,
the <b>XBD</b> specification, <a href="../xbd/locale.html"><b>Locale</b>&nbsp;</a>,
<i><a href="http://www.opengroup.org/platform/resolutions/bwg98-006.html">Base Working Group Resolution # bwg98-006</a></i>.
</blockquote><h4>DERIVATION</h4><blockquote>
Derived from Issue 1 of the SVID.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>

